use axum::{
    extract::State,
    response::Json,
};
use serde::Serialize;
use std::{sync::Arc, time::{SystemTime, UNIX_EPOCH}};

use crate::auth::AuthService;
use crate::monitoring::{MonitoringService, MonitoringStats};

// 使用统一的AppState定义
pub use crate::AppState;

#[derive(Serialize)]
pub struct HealthResponse {
    pub status: String,
    pub timestamp: String,
}

#[derive(Serialize)]
pub struct SystemInfoResponse {
    pub version: String,
    pub uptime: u64,
    pub environment: String,
}

// 健康检查端点
pub async fn health_check() -> Json<HealthResponse> {
    let timestamp = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap()
        .as_secs();
    
    Json(HealthResponse {
        status: "healthy".to_string(),
        timestamp: format!("{}", timestamp),
    })
}

// 系统信息端点
pub async fn system_info() -> Json<SystemInfoResponse> {
    // 简化实现，实际应该获取真实系统信息
    Json(SystemInfoResponse {
        version: "1.0.0".to_string(),
        uptime: 0, // 实际应该计算启动时间
        environment: "development".to_string(),
    })
}

// 监控统计端点
pub async fn get_monitoring_stats(
    State(state): State<Arc<AppState>>,
) -> Json<MonitoringStats> {
    let stats = state.monitoring_service.stats.get_stats();
    Json(stats)
}

// 网关代理统计端点
pub async fn get_gateway_stats(
    State(state): State<Arc<AppState>>,
) -> Json<crate::gateway_proxy::ProxyStats> {
    let stats = state.gateway_proxy.get_stats().await;
    Json(stats)
}

// 网关配置端点
pub async fn get_gateway_config(
    State(state): State<Arc<AppState>>,
) -> Json<crate::gateway_proxy::GatewayConfig> {
    let config = state.gateway_proxy.config.clone();
    Json(config)
}